package com.yuju.account.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuju.account.dao.IUnitAccountBookDao;
import com.yuju.account.dao.entity.UnitAccountBook;
import com.yuju.account.enums.AccountTypeEnum;
import com.yuju.account.enums.UnitAccountBookReturnCodeEnum;
import com.yuju.account.enums.UnitAccountReturnCodeEnum;
import com.yuju.account.model.UnitAccountBookModel;
import com.yuju.account.model.UnitAccountModel;
import com.yuju.account.service.IUnitAccountBookService;
import com.yuju.account.service.IUnitAccountService;
import com.yuju.common.PageModel;
import com.yuju.common.core.dao.IBaseDao;
import com.yuju.common.core.service.AbstractBaseService;
import com.yuju.enums.StatusEnum;
import com.yuju.exception.BusinessException;
import com.yuju.util.Utils;

/**
 * 类名：UnitAccountBookServiceImpl  <br />
 *
 * 功能：
 *
 * @author : jiaokn <br />
 * @Date : 2017年11月27日 上午11:14:50  <br />
 * @version : 1.1 <br />
 */
@Service
public class UnitAccountBookServiceImpl extends AbstractBaseService<UnitAccountBookModel, UnitAccountBook, String> implements IUnitAccountBookService {
	
	@Autowired
	private IUnitAccountBookDao unitAccountBookDao;
	
	@Autowired
	private IUnitAccountService unitAccountService;
	
	@Override
	public IBaseDao<UnitAccountBookModel, UnitAccountBook, String> getDao() {
		// TODO Auto-generated method stub
		return unitAccountBookDao;
	}
	
	/**
	 * 功能:重写save方法 <br/>
	 * 
	 * 重写：jiaokn <br/>
	 * 
	 * @Date ：2017年12月9日 下午5:55:33<br/>
	 * 
	 * @param unitAccountBookModel
	 * @return <br/>
	 * @see com.yuju.common.core.service.AbstractBaseService#save(java.lang.Object)
	 */
	@Override
	public int save(UnitAccountBookModel unitAccountBookModel){
		unitAccountBookModel.setId(Utils.getUUID());
		unitAccountBookModel.setGmtCreate(new Date());
		unitAccountBookModel.setGmtModified(new Date());
		return super.save(unitAccountBookModel);
	}

	/**
	 * 功能:新增帐套并返回主键 <br/>
	 * 
	 * 重写：jiaokn <br/>
	 * 
	 * @Date ：2017年12月9日 下午2:49:01<br/>
	 * 
	 * @param unitAccountBookModel
	 * @return <br/>
	 * @see com.yuju.account.service.IUnitAccountBookService#saveAndGetId(com.yuju.account.model.UnitAccountBookModel)
	 */
	@Override
	public String saveAndGetId(UnitAccountBookModel unitAccountBookModel) {
		unitAccountBookModel.setId(Utils.getUUID());
		unitAccountBookModel.setGmtCreate(new Date());
		unitAccountBookModel.setGmtModified(new Date());
		if (super.save(unitAccountBookModel) < 1) {
			throw new BusinessException(UnitAccountBookReturnCodeEnum.UNITACCOUNTBOOK_SAVE_FAILURE.getCode(),
					UnitAccountBookReturnCodeEnum.UNITACCOUNTBOOK_SAVE_FAILURE.getDesc());
		}
		return unitAccountBookModel.getId();
	}

	/**
	 * 功能:新增单位帐套 <br/>
	 * 
	 * 1.新增单位帐套并得到其id
	 * 2.更新账务的帐套id
	 * 
	 * 重写：jiaokn <br/>
	 * 
	 * @Date ：2017年12月9日 下午5:06:03<br/>
	 * 
	 * @param accountBookModel
	 * @param unitAccountIdList
	 * @return <br/>
	 * @see com.yuju.account.service.IUnitAccountBookService#insert(com.yuju.account.model.UnitAccountBookModel, java.util.ArrayList)
	 */
	@Override
	public int saveUnitAccountBook(UnitAccountBookModel unitAccountBookModel, ArrayList<String> unitAccountIdList) {
		String id = this.saveAndGetId(unitAccountBookModel);
		int i = 1;
		if(Utils.isNull(id)){
			throw new BusinessException(UnitAccountBookReturnCodeEnum.UNITACCOUNTBOOK_SAVE_FAILURE.getCode(),
					UnitAccountBookReturnCodeEnum.UNITACCOUNTBOOK_SAVE_FAILURE.getDesc());
		}
		for (String unitAccountId : unitAccountIdList) {
			UnitAccountModel unitAccountModel = new UnitAccountModel();
			unitAccountModel.setId(unitAccountId);
			unitAccountModel.setAccountBookId(id);
			unitAccountModel.setUpdateUserId(unitAccountBookModel.getCreateUserId());
			unitAccountModel.setGmtModified(new Date());
			if(unitAccountService.updateSelective(unitAccountModel)<1){
				throw new BusinessException(UnitAccountReturnCodeEnum.UNITACCOUNT_UPDATE_FAILURE.getCode(),
						UnitAccountReturnCodeEnum.UNITACCOUNT_UPDATE_FAILURE.getDesc());
			}
		}
		return i;
	}

	/**
	 * 功能:单位账务编辑帐套 <br/>
	 * 
	 * 1.查找帐套原账务
	 * 2.将原账务的帐套id更新为空
	 * 3.更新帐套新账务的帐套id
	 * 
	 * 重写：jiaokn <br/>
	 * 
	 * @Date ：2017年12月11日 上午11:33:35<br/>
	 * 
	 * @param unitAccountBookModel
	 * @param unitAccountIdList
	 * @return <br/>
	 * @see com.yuju.account.service.IUnitAccountBookService#update(com.yuju.account.model.UnitAccountBookModel, java.util.ArrayList)
	 */
	@Override
	public int updateUnitAccountBook(UnitAccountBookModel unitAccountBookModel, ArrayList<String> unitAccountIdList) {
		UnitAccountModel unitAccountModel = new UnitAccountModel();
		unitAccountModel.setAccountBookId(unitAccountBookModel.getId());
		unitAccountModel.setAccountType(AccountTypeEnum.ACCOUNT_TYPE_YS.getValue());
		List<UnitAccountModel> list = unitAccountService.list(unitAccountModel);
		for (UnitAccountModel unitAccountModelOld : list) {
			unitAccountModelOld.setAccountBookId(null);
			unitAccountModelOld.setUpdateUserId(unitAccountBookModel.getUpdateUserId());
			unitAccountModelOld.setGmtModified(new Date());
			if(unitAccountService.update(unitAccountModelOld)<1){
				throw new BusinessException(UnitAccountReturnCodeEnum.UNITACCOUNT_UPDATE_FAILURE.getCode(),
						UnitAccountReturnCodeEnum.UNITACCOUNT_UPDATE_FAILURE.getDesc());
			}
		}
		
		for (String unitAccountId : unitAccountIdList) {
			UnitAccountModel unitAccountModelNew = new UnitAccountModel();
			unitAccountModelNew.setId(unitAccountId);
			unitAccountModelNew.setAccountBookId(unitAccountBookModel.getId());
			unitAccountModelNew.setUpdateUserId(unitAccountBookModel.getUpdateUserId());
			unitAccountModelNew.setGmtModified(new Date());
			if(unitAccountService.updateSelective(unitAccountModelNew)<1){
				throw new BusinessException(UnitAccountReturnCodeEnum.UNITACCOUNT_UPDATE_FAILURE.getCode(),
						UnitAccountReturnCodeEnum.UNITACCOUNT_UPDATE_FAILURE.getDesc());
			}
		}
		
		return this.updateSelective(unitAccountBookModel);
		
	}

	/**
	 * 功能:查询帐套列表 并总结账务 <br/>
	 * 
	 * 重写：jiaokn <br/>
	 * 
	 * @Date ：2017年12月15日 下午12:00:23<br/>
	 * 
	 * @param unitAccountBookModel
	 * @param currentPage
	 * @param rows
	 * @return <br/>
	 * @see com.yuju.account.service.IUnitAccountBookService#listAndSum(com.yuju.account.model.UnitAccountBookModel, java.lang.Integer, java.lang.Integer)
	 */
	@Override
	public PageModel listAndSum(UnitAccountBookModel unitAccountBookModel, Integer currentPage, Integer rows) {
		return unitAccountBookDao.listAndSum(unitAccountBookModel, currentPage, rows);
	}

	/**
	 * 功能:取消结账 <br/>
	 * 
	 * 1.更新帐套下账务结账状态为未结
	 * 2.更新帐套结账状态为未结
	 * 
	 * 重写：jiaokn <br/>
	 * 
	 * @Date ：2017年12月27日 下午4:42:12<br/>
	 * 
	 * @param id
	 * @return <br/>
	 * @see com.yuju.account.service.IUnitAccountBookService#reduce(java.lang.String)
	 */
	@Override
	public int updateSettle(UnitAccountBookModel unitAccountBookModel) {
		
		Map<String, Object> map = new HashMap<>();
		map.put("accountBookId", unitAccountBookModel.getId());
		map.put("isSettle", StatusEnum.FALSE.getValue());
		map.put("updateUserId", unitAccountBookModel.getUpdateUserId());
		map.put("gmtModified",new Date());
		int i = unitAccountBookDao.updateSettle(map);
		if(i<1){
			throw new BusinessException(UnitAccountReturnCodeEnum.UNITACCOUNT_UPDATE_FAILURE.getCode(),
					UnitAccountReturnCodeEnum.UNITACCOUNT_UPDATE_FAILURE.getDesc());
		}
		
		UnitAccountBookModel updateModel = new UnitAccountBookModel();
		updateModel.setId(unitAccountBookModel.getId());
		updateModel.setIsSettle(StatusEnum.FALSE.getValue());
		updateModel.setUpdateUserId(unitAccountBookModel.getUpdateUserId());
		updateModel.setGmtModified(new Date());
		
		return this.updateSelective(updateModel);
	}

}
